package dwl.graduation.design.controller;

import dwl.graduation.design.domain.*;
import dwl.graduation.design.service.RoleService;
import dwl.graduation.design.utils.MessageConstant;
import dwl.graduation.design.utils.PageResult;
import dwl.graduation.design.utils.QueryPageBean;
import dwl.graduation.design.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping(path = "/role")
public class RoleController {
    private final RoleService roleService;

    @Autowired
    public RoleController(RoleService roleService){
        this.roleService=roleService;
    }


    @RequestMapping(value = "/findRoleList", method = RequestMethod.POST)
    @PreAuthorize("hasRole('ADMIN')")
    public PageResult findRoleList(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findRoleList(queryPageBean);
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @PreAuthorize("hasRole('ADMIN')")
    public Result insertRole(@RequestBody RoleInsertRequest request) {
        try {
            boolean flag = roleService.insertRoleInfo(request);
            if (flag) {
                return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
            } else {
                return new Result(false, MessageConstant.ADD_ROLE_FAIL);
            }
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @PreAuthorize("hasRole('ADMIN')")
    public Result deleteRole(@RequestParam("id") Integer id) {
        try {
            roleService.deleteRole(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasRole('ADMIN')")
    public Result updateRole(@RequestBody RoleUpdateRequest roleUpdateRequest) {
        try {
            boolean flag = roleService.updateRole(roleUpdateRequest);
            if(flag){
                return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
            }else{
                return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
            }
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    @RequestMapping(value = "/insertPermission",method = RequestMethod.POST)
    public Result insertRoleForUser(@RequestBody InsertPermissionForRoleRequest insertRoleForUserRequest){
        try{
            boolean flag = roleService.insertPermissionForRole(insertRoleForUserRequest);
            if(flag){
                return new Result(true, MessageConstant.ADD_ROLE_PERMISSION_SUCCESS);
            }else{
                return new Result(false, MessageConstant.ADD_ROLE_PERMISSION_FAIL);
            }
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_ROLE_PERMISSION_FAIL);
        }
    }

    @RequestMapping(value = "/findRoleById",method = RequestMethod.GET)
    public Result getUserInfoByUserId(Integer id){
        try{
            Role role = roleService.findRoleById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }
}
